package me.xiaosiqi.service.store.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.xiaosiqi.dao.store.QuestionItemDao;
import me.xiaosiqi.domain.store.QuestionItem;
import me.xiaosiqi.factory.MapperFactory;
import me.xiaosiqi.service.store.QuestionItemService;
import me.xiaosiqi.utils.BeanUtil;
import me.xiaosiqi.utils.FileUtil;
import me.xiaosiqi.utils.TransactionUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author xiaosiqi
 * @date 2021/6/4 19:18
 */

public class QuestionItemServiceImpl implements QuestionItemService {

    @Override
    public void save(QuestionItem questionItem) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            String s = UUID.randomUUID().toString();
            questionItem.setId(s);

            questionItemDao.save(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(QuestionItem questionItem) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            questionItemDao.delete(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update(QuestionItem questionItem) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            questionItemDao.update(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public QuestionItem findById(String id) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            return questionItemDao.findById(id);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<QuestionItem> findByQuestionId(String questionId) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            return questionItemDao.findByQuestionId(questionId);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public QuestionItem saveOrEdit(List<FileItem> fileItems, String path) {
        try {
            //数据部分，放入实体类
            QuestionItem questionItem = BeanUtil.fillBean(fileItems, QuestionItem.class);
            //拿到文件内容，并获取文件名部分
            String returnName = FileUtil.uploadFile(path, UUID.randomUUID().toString(), fileItems);
            //将文件名写入实体类
            if (StringUtils.isNoneBlank(returnName)){
             //   questionItem.setPicture(uploadPath+questionItem.getQuestionId()+"/"+returnName);
            }
            return questionItem;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


}

